package com.mallcloud.mall.app.api.entity.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

@ApiModel(value = "WxApiPhoneNumberVO", description = "手机号验证")
@Data
public class WxApiPhoneNumberVO implements Serializable {
	private static final long serialVersionUID = 1L;

	private static final Integer SUCCESS_CODE = 0;

	@ApiModelProperty("用户手机号信息")
	@JSONField(name = "phone_info")
	private PhoneInfo phoneInfo;

	@ApiModel(value = "PhoneInfo", description = "用户手机号信息")
	@Data
	public static class PhoneInfo {
		@ApiModelProperty("用户绑定的手机号")
		private String phoneNumber;
		@ApiModelProperty("没有区号的手机号")
		private String purePhoneNumber;
		@ApiModelProperty("区号")
		private String countryCode;
	}


	@ApiModelProperty("错误码")
	@JSONField(name = "errcode")
    private Integer errCode;
	@ApiModelProperty("错误码描述")
    private String errCodeDesc;
	public String getErrCodeDesc() {
		if(Objects.isNull(errCode)){
			return "";
		}
		return CodeEnum.getDecs(this.errCode);
	}

	@ApiModelProperty("错误信息")
	@JSONField(name = "errmsg")
    private String errMsg;

	@JsonIgnore
	@JSONField(serialize = false)
	public Boolean isSuccess(){
		return SUCCESS_CODE.equals(this.errCode);
	}

	@Getter
	@AllArgsConstructor
	public enum CodeEnum{
		code_1(40029, "code 无效"),
		code_4(-1, "系统繁忙，此时请开发者稍候再试"),
		;
		private Integer code;
		private String description;
		public static String getDecs(Integer code) {
			return Arrays.stream(values()).filter(x -> x.getCode().equals(code)).findFirst().get().getDescription();
		}

	}
}
